package wuke.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import wuke.dao.ZyDao;
import wuke.model.Zyb;
import wuke.utils.HibernateSessionFactory;

public class ZyDaoImp implements ZyDao {

	public List getAll() {

		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction ts = session.beginTransaction();
			List list = session.createSQLQuery("from Zyb").list();
			ts.commit();
			HibernateSessionFactory.closeSession();
			return list;
		} catch (HibernateException e) {

			e.printStackTrace();
			return null;
		}
	}

	public Zyb getOneZy(Integer zyId) {

		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction ts = session.beginTransaction();
			SQLQuery query = session.createSQLQuery("from Zyb where id=?");
			query.setParameter(0, zyId);
			query.setMaxResults(1);
			Zyb zy = (Zyb) query.uniqueResult();
			ts.commit();
			HibernateSessionFactory.closeSession();
			return zy;
		} catch (HibernateException e) {

			e.printStackTrace();
			return null;
		}

	}

}
